﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ProjectHT.YKDGVB2012.Core.Datacontract
{
    public class CommentServices : IComment
    {

        private SPSite osite;
        private SPWeb oweb;
        public CommentServices(SPContext context)
        {
            osite = context.Site;
            oweb = osite.OpenWeb();
        }
        #region Implementation of IComment
        /// <summary>
        /// Get all SPlistItem in List Comment to Object Comments
        /// </summary>
        /// <returns></returns>
        public List<Comments> GetAllComment()
        {

            var datalist = new List<Comments>();
            if (oweb != null)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate
                                                             {
                                                                 var olist = oweb.Lists[Common.CommentList.COMMENTLIST];
                                                                 datalist.AddRange(from SPListItem spListItem in olist.Items
                                                                                   select new Comments
                                                                                              {
                                                                                                  Title = spListItem.Title,
                                                                                                  ID = spListItem.ID,
                                                                                                  FullName = String.Format("{0}", spListItem[Common.CommentList.FIELDFULLNAME]),
                                                                                                  IDMain = Convert.ToInt32(spListItem[Common.CommentList.FIELDMAINID]),
                                                                                                  PhoneNumber = String.Format("{0}", spListItem[Common.CommentList.FIELDPHONE]),
                                                                                                  Email = String.Format("{0}", spListItem[Common.CommentList.FIELDEMAIL]),
                                                                                                  Fax = String.Format("{0}", spListItem[Common.CommentList.FIELDFAX]),
                                                                                                  Contents = String.Format("{0}", spListItem[Common.CommentList.FIELDCONTENTS]),
                                                                                                  Address = String.Format("{0}", spListItem[Common.CommentList.FIELDADDRESS]),
                                                                                                  CreatedDate = Convert.ToDateTime(spListItem["Created"]),
                                                                                                  Statust = (bool)spListItem[Common.CommentList.FIELDSTATUS]
                                                                                              });
                                                             });
                }
                catch (SPException ex)
                {
                    Helper.SystemWriteLog(ex.Message);
                }
            }
            return datalist;

        }

        public Comments GetItemById(int itemid)
        {
            return GetAllComment().FirstOrDefault(p => p.ID == itemid);
        }

        public SPListItem GetItem(int itemid)
        {
            return GetitemById(itemid);
        }

        private SPListItem GetitemById(int id)
        {
            var datalist = new List<SPListItem>();
            if (oweb != null)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate
                    {
                        var olist = oweb.Lists[Common.CommentList.COMMENTLIST];
                        datalist.AddRange(olist.Items.Cast<SPListItem>());
                    });
                }
                catch (SPException ex)
                {
                    Helper.SystemWriteLog(ex.Message);
                }
            }
            return datalist.FirstOrDefault(p => p.ID == id);
        }
        public void Delete(int itemId, SPContext context)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate
                        {

                            oweb.AllowUnsafeUpdates = true;
                            var item = GetitemById(itemId);
                            if (item != null)
                            {
                                item.Delete();
                            }
                            oweb.AllowUnsafeUpdates = false;
                        });

        }

        public void Update(Comments comments)
        {
            try
            {

         
            SPSecurity.RunWithElevatedPrivileges(delegate
                                                     {

                                                         oweb.AllowUnsafeUpdates = true;

                                                         var item = GetitemById(comments.ID);
                                                         if (item != null)
                                                         {
                                                             item[Common.CommentList.FIELDTITLE] = comments.Title;
                                                             item[Common.CommentList.FIELDADDRESS] = comments.Address;
                                                             item[Common.CommentList.FIELDEMAIL] = comments.Email;
                                                             item[Common.CommentList.FIELDFAX] = comments.Fax;
                                                             item[Common.CommentList.FIELDFULLNAME] = comments.FullName;
                                                             item[Common.CommentList.FIELDPHONE] = comments.PhoneNumber;
                                                             item[Common.CommentList.FIELDCONTENTS] = comments.Contents;
                                                             item[Common.CommentList.FIELDSTATUS] = comments.Statust;
                                                             item.Update();
                                                         }
                                                         oweb.AllowUnsafeUpdates = false;
                                                     });
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                oweb.Dispose();
            }
        }

        public void Create(Comments comments)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
